<?php
/*------------------------------------------------------------------------
# com_vportfolio - Vportfolio component
# ------------------------------------------------------------------------
# author    Vsmart Extensions
# copyright Copyright (C) 2010 www.vsmart-extensions.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.vsmart-extensions.com
# Technical Support:  Forum - http://www.vsmart-extensions.com
-------------------------------------------------------------------------*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.view');

/**
 * @package		Joomla
 * @subpackage	Vportfolio
 */
class PortfolioViewCategory extends JView
{
	function display($tpl = null)
	{
		global $mainframe, $option, $Itemid;
		$pathway	= &$mainframe->getPathway();
		$pathway->setPathway(NULL);
		$modelConfig = &JModel::getInstance( 'vportfolioConfigComponentModel' );
		$config = $modelConfig->getConfigParams();

		$uri 	  =& JFactory::getURI();
		$model	  = &$this->getModel();
		//add more
		$document =& JFactory::getDocument();
		//url	
		
		$document->addScript(JURI::base().'components/com_vportfolio/js/jquery.js');	
		$document->addScript(JURI::base().'components/com_vportfolio/js/prettyPhoto.js');	
		$document->addStyleSheet(JURI::base().'components/com_vportfolio/prettyPhoto.css');
		$document->addStyleSheet(JURI::base().'components/com_vportfolio/vportfolio.css');		
		
		
		$pparams = &$mainframe->getParams('com_vportfolio');
		$categoryId = $pparams->get( 'id' );
		if($categoryId ==''){
			// Selected Request vars
			$categoryId			= JRequest::getVar('catid',				0,				'', 'int');
		}
		$limit				= JRequest::getVar('limit',				$mainframe->getCfg('list_limit'),	'', 'int');
		$limitstart			= JRequest::getVar('limitstart',		0,				'', 'int');
		$filter_order		= JRequest::getVar('filter_order',		'i.id',	'', 'cmd');
		$filter_order_Dir	= JRequest::getVar('filter_order_Dir',	'ASC',			'', 'word');

		// query options
		$options['category_id']	= $categoryId;
		$options['limit']		= $limit;
		$options['limitstart']	= $limitstart;
		$options['order by'] = " i.feature DESC, i.date_created DESC ";

		$categories	= $model->getCategories( $options );
		//Get list Portpolio
		$items		= $model->getPortfolios( $options );
		$total 		= $model->getPortfolioCount( $options );
		$itemPortpolioArray = array();
		for($i=0;$i<sizeof($items);$i++){
			$items[$i]->image = $model->getDefaultImage($items[$i]->id);
			$items[$i]->link = JRoute::_("index.php?option=com_vportfolio&task=view&id=".$items[$i]->id."&Itemid=".$Itemid);
					
			$itemPortpolioArray[$i] = $model->getImages($items[$i]->id);
		}
		// find current category
		// TODO: Move to model
		$category = null;
		foreach ($categories as $i => $_cat)
		{
			if ($_cat->id == $categoryId) {
				$category = &$categories[$i];
				break;
			}
		}
		if ($category == null) {
			$db = &JFactory::getDBO();
			$category =& JTable::getInstance( 'category' );
		}

		// Set the page title and pathway
		if ($category->name)
		{
			// Add the category breadcrumbs item
			$document->setTitle(JText::_('Portfolio').' - '.$category->name);
			$pathway->addItem($category->name, '');
		} else {
			$document->setTitle(JText::_('Portfolio'));
		}

		// table ordering
		$lists['order_Dir'] = $filter_order_Dir;
		$lists['order'] = $filter_order;
		$selected = '';

		jimport('joomla.html.pagination');
		$pagination = new JPagination($total, $limitstart, $limit);
		$this->assignRef('items',		$items);
		$this->assignRef('itemPortpolioArray',		$itemPortpolioArray);
		
		$this->assignRef('lists',		$lists);
		$this->assignRef('pagination',	$pagination);
		$this->assignRef('category',	$category);
		$this->assignRef('params',		$pparams);
		$this->assignRef('config',		$config);

		$this->assign('action',		$uri->toString());

		parent::display($tpl);
	}
}